#!/usr/bin/env bash
echo -e "\033[34m ####################################################\033[0m"
echo -e "\033[34m ##   author：侯勇                                 ##\033[0m"
echo -e "\033[34m ##   mail：yhouwork@163.com                       ##\033[0m"
echo -e "\033[34m ##   Linux version：  Cent OS7                    ##\033[0m"
echo -e "\033[34m ##   ES    version ： 7.2.0                       ##\033[0m"
echo -e "\033[34m ##   kibana version ： 7.2.0                      ##\033[0m"
echo -e "\033[34m ##   date ：2019/7/09                             ##\033[0m"
echo -e "\033[34m ##   ps ：CentOS6与CentOS7防火墙端口开放命令不同  ##\033[0m"
echo -e "\033[34m ####################################################\033[0m"
###############################安装包准备###############################
adduser elasticsearch;
firewall-cmd --zone=public --add-port=9200/tcp --permanent
firewall-cmd --zone=public --add-port=5601/tcp --permanent
firewall-cmd --reload
systemctl restart firewalld.service
cd /;
yum -y install wget;
yum -y install tar;
if [ ! -e "/app/elasticsearch-7.2.0-linux-x86_64.tar.gz" ];
 then
    if [ ! -d   "/app"  ];
        then
        mkdir /app
    fi
    cd /app;
    echo -e "\033[34m 下载安装包  \033[0m"
    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.0-linux-x86_64.tar.gz
    cd -;
 fi

if [ ! -e "/app/kibana-7.2.0-linux-x86_64.tar.gz" ];
 then
    if [ ! -d   "/app"  ];
        then
        mkdir /app
    fi
    cd /app;
    echo -e "\033[34m 下载安装包  \033[0m"
    wget https://artifacts.elastic.co/downloads/kibana/kibana-7.2.0-linux-x86_64.tar.gz
    cd -;
 fi
cd /usr/local;
cp /app/elasticsearch-7.2.0-linux-x86_64.tar.gz /usr/local
tar -zxvf elasticsearch-7.2.0-linux-x86_64.tar.gz
tar -zxvf elasticsearch-7.2.0-linux-x86_64.tar.gz
mv elasticsearch-7.2.0 elasticsearch

cp /app/kibana-7.2.0-linux-x86_64.tar.gz /usr/local
tar -zxvf kibana-7.2.0-linux-x86_64.tar.gz
mv kibana-7.2.0-linux-x86_64   kibana


cd /
chown -R elasticsearch:elasticsearch /usr/local/kibana
chown -R elasticsearch:elasticsearch /usr/local/elasticsearch


#替换datadir
mycnfdir=/usr/local/elasticsearch/config/elasticsearch.yml
#声明行
num=0;
numa=0;
nullnum=;
num=$(grep -n '\#http\.port\:\ 9200'  $mycnfdir| awk -F ':' '{print $1}')
   echo -e "\033[34m =================》port存在  $num《=================  \033[0m"
# 判断mysql配置是否存在
if  [ ! "$num" = ""   ] ;then
    echo -e "\033[34m =================》判断mysql配置存在  $num《=================  \033[0m"
    sed -i 's/\#http\.port\:\ 9200/http\.port\:\ 9200/g' $mycnfdir
    numa=$num"a"
    echo -e "\033[34m =================》datadir此变量不存在  进行插入  $numa=================  \033[0m"
    sed -i $numa'http\.host\:\ 0\.0\.0\.0' $mycnfdir ;
    num=$nullnum;
fi


#替换datadir
mycnfdir=/usr/local/kibana/config/kibana.yml
#声明行
num=0;
numa=0;
nullnum=;
num=$(grep -n '\#server\.host\: \"localhost\"'  $mycnfdir| awk -F ':' '{print $1}')
   echo -e "\033[34m =================》port存在  $num《=================  \033[0m"
# 判断mysql配置是否存在
if  [ ! "$num" = ""   ] ;then
    echo -e "\033[34m =================》判断mysql配置存在  $num《=================  \033[0m"
    sed -i 's/\#server\.host\: \"localhost\"/server\.host\: \"0\.0\.0\.0\"/g' $mycnfdir
    num=$nullnum;
fi

su elasticsearch -s /usr/local/elasticsearch/bin/elasticsearch &
su elasticsearch -s /usr/local/kibana/bin/kibana &
